<?php

//Loader::loadClass('SecurityUtilEx', "modules/VoteThailandPoll/pnincludes");

function array_msort($array, $cols)
{
    $colarr = array();
    foreach ($cols as $col => $order) {
        $colarr[$col] = array();
        foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
    }
    $params = array();
    foreach ($cols as $col => $order) {
        $params[] =& $colarr[$col];
        $params = array_merge($params, (array)$order);
    }
    call_user_func_array('array_multisort', $params);
    $ret = array();
    $keys = array();
    $first = true;
    foreach ($colarr as $col => $arr) {
      foreach ($arr as $k => $v) {
          if ($first) { $keys[$k] = substr($k,1); }
          $k = $keys[$k];
          if (!isset($ret[$k])) $ret[$k] = $array[$k];
          $ret[$k][$col] = $array[$k][$col];
      }
      $first = false;
    }
    return $ret;
}

/**
* //////////////////////////////////////////////////
* auto execute , for initialize config variable
* this function auto call every page has been fetch
*/
function _autoexecute(){
  
}

function VoteDataCenter_ws_permission()
{
  // Security check
  //we are allow for admin access level , see in config.php variable name ACCESS_EDIT
  if (!SecurityUtil::checkPermission('VoteDataCenter::', '::', ACCESS_EDIT)) {
      LogUtil::registerPermissionError(pnModUrl('Users','user','loginscreen'));
  }
}


/**
 * Main user function . the user side page controller
 * @author Chayakon PONGSIRI
 * @return string HTML string
 */
function VoteDataCenter_ws_main() {
    //_autoexecute();
    return VoteThailandPoll_xml_page();
}

function VoteDataCenter_ws_page()
{
  //retrun null;
}

function VoteDataCenter_ws_getProvinceList()
{
  $list  = FormUtil::getPassedValue ('list', null , 'GET');
  $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
  $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','ProvinceArray', false)))
  return LogUtil::registerError ("Unable to load class [ProvinceArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  if(isset($list))
  {
    if (method_exists($objectArray,'genFilter')){
      $where = $objectArray->genFilter();
    }
  }
  if(isset($desc)||isset($asc)){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }

  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $value)
  {
    
    $xmlChild = $xml->createElement("data");
    
    foreach($value as $key=>$val){
      $xmlNode = $xml->createElement($key,$val);
      $xmlChild->appendChild($xmlNode);
    
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getDistrictListByProvinceId()
{
  $id  = FormUtil::getPassedValue ('id', FALSE , 'GET');
  $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
  $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
  
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','DistrictArray', false)))
  return LogUtil::registerError ("Unable to load class [DistrictArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  if (method_exists($objectArray,'genFilterDistrictFromProvince')){
      $where = $objectArray->genFilterDistrictFromProvince();
  }
  if(isset($desc)||isset($asc)){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }

  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $value)
  {
    
    $xmlChild = $xml->createElement("data");
    
    foreach($value as $key=>$val){
      $xmlNode = $xml->createElement($key,$val);
      $xmlChild->appendChild($xmlNode);
    
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getDistrictList()
{
  $list  = FormUtil::getPassedValue ('list', null , 'GET');
  $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
  $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
  
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','DistrictArray', false)))
  return LogUtil::registerError ("Unable to load class [DistrictArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  if(isset($list)){
    if (method_exists($objectArray,'genFilter')){
      $where = $objectArray->genFilter();
    }
  }
  
  if(isset($desc)||isset($asc)){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }

  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $value)
  {
    
    $xmlChild = $xml->createElement("data");
    
    foreach($value as $key=>$val){
      $xmlNode = $xml->createElement($key,$val);
      $xmlChild->appendChild($xmlNode);
    
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getTambonListByDistrictId()
{
  $id  = FormUtil::getPassedValue ('id', FALSE , 'GET');
  $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
  $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
  
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','TambonArray', false)))
  return LogUtil::registerError ("Unable to load class [TambonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  if (method_exists($objectArray,'genFilterTambonFromDistrict')){
      $where = $objectArray->genFilterTambonFromDistrict();
  }
  if(isset($desc)||isset($asc)){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }

  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $value)
  {
    
    $xmlChild = $xml->createElement("data");
    
    foreach($value as $key=>$val){
      $xmlNode = $xml->createElement($key,$val);
      $xmlChild->appendChild($xmlNode);
    
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getTambonList()
{
  $list  = FormUtil::getPassedValue ('list', null , 'GET');
  $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
  $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
  
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','TambonArray', false)))
  return LogUtil::registerError ("Unable to load class [TambonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  if(isset($list)){
    if (method_exists($objectArray,'genFilterTambonFromDistrict')){
      $where = $objectArray->genFilterTambonFromDistrict();
    }
  }
  
  if(isset($desc)||isset($asc)){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }

  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $value)
  {
    
    $xmlChild = $xml->createElement("data");
    
    foreach($value as $key=>$val){
      $xmlNode = $xml->createElement($key,$val);
      $xmlChild->appendChild($xmlNode);
    
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getVillageListByTambonId()
{
  $id  = FormUtil::getPassedValue ('id', FALSE , 'GET');
  $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
  $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
  
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','VillageArray', false)))
  return LogUtil::registerError ("Unable to load class [VillageArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  if (method_exists($objectArray,'genFilterVillageFromTambon')){
      $where = $objectArray->genFilterVillageFromTambon();
  }
  if(isset($desc)||isset($asc)){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }

  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $value)
  {
    
    $xmlChild = $xml->createElement("data");
    
    foreach($value as $key=>$val){
      $xmlNode = $xml->createElement($key,$val);
      $xmlChild->appendChild($xmlNode);
    
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getVillageList()
{
  $list  = FormUtil::getPassedValue ('list', null , 'GET');
  $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
  $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
  
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','VillageArray', false)))
  return LogUtil::registerError ("Unable to load class [VillageArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  if(isset($list)){
    if (method_exists($objectArray,'genFilterVillageFromTambon')){
      $where = $objectArray->genFilterVillageFromTambon();
    }
  }
  
  if(isset($desc)||isset($asc)){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }

  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $value)
  {
    
    $xmlChild = $xml->createElement("data");
    
    foreach($value as $key=>$val){
      $xmlNode = $xml->createElement($key,$val);
      $xmlChild->appendChild($xmlNode);
    
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getHouseList()
{
  $list  = FormUtil::getPassedValue ('list', null , 'GET');
  $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
  $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
  
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','HouseNoArray', false)))
  return LogUtil::registerError ("Unable to load class [HouseNoArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  if(isset($list)){
    if (method_exists($objectArray,'genFilterHouseFromVillage')){
      $where = $objectArray->genFilterHouseFromVillage();
    }
  }
  
  if(isset($desc)||isset($asc)){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }

  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $value)
  {
    
    $xmlChild = $xml->createElement("data");
    
    foreach($value as $key=>$val){
      $xmlNode = $xml->createElement($key,$val);
      $xmlChild->appendChild($xmlNode);
    
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getHouseListByProvinceId()
{
  $id  = FormUtil::getPassedValue ('id', null , 'REQUEST');
  $desc  = FormUtil::getPassedValue ('desc', null, 'REQUEST');
  $asc  = FormUtil::getPassedValue ('asc', null, 'REQUEST');
  $page = FormUtil::getPassedValue ('page', 1, 'REQUEST');
  $pageSize = 2000;
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }else if(!is_numeric($id)){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","Invalid ardument: [id].");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','HouseNoArray', false)))
  return LogUtil::registerError ("Unable to load class [HouseNoArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  

  if (method_exists($objectArray,'genFilterHouseListByProvinceId')){
    $where = $objectArray->genFilterHouseListByProvinceId();
  }

  
  if((isset($desc)||isset($asc))&&($desc!='no'&&$asc!='no')){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  //Spilt page
  $totalItems  = $objectArray->getCount($where , true);
  $totalPages  = ceil($totalItems/$pageSize);
  $nowPage = $page;
  if($page>0){
    $page--;
  }
  $page = $page*$pageSize;
  
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  $i=0;
  foreach($objDataArray AS $object){
  
    $object2[$i] = $object;
    preg_match('/(?P<house_no>\w+)\/(?P<sub_house_no>\d+)/',$object['no'],$matches);
    
    $object2[$i]['sub_house_no'] = $matches['sub_house_no'] ? $matches['sub_house_no'] : "0";
    $object2[$i]['house_no'] = $matches['house_no'] ? $matches['house_no'] : $object['no'];

  $i++;
  }
  
  if($asc=='no'||(!isset($asc)&&!isset($desc))){
    $object2 = array_msort($object2,array('vil_id'=>SORT_ASC, 'vil_moo'=>SORT_ASC, 'house_no'=>SORT_ASC, 'sub_house_no'=>SORT_ASC));
  }else if ($desc=='no'){
    $object2 = array_msort($object2,array('vil_id'=>SORT_DESC, 'vil_moo'=>SORT_DESC, 'house_no'=>SORT_DESC, 'sub_house_no'=>SORT_DESC));
  }
  
  $previous = $nowPage<=1 ? "" : $nowPage-1;
  $next = $nowPage>=$totalPages ? "" : $nowPage+1;
  //Assign some attribute for xml
  $object2['totalItems'] = $totalItems;
  $object2['totalPages'] = $totalPages;
  $object2['nowPage'] = $nowPage;
  $object2['next'] = $next;
  $object2['previous'] = $previous;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($object2 as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getHouseListByDistrictId()
{
  $id  = FormUtil::getPassedValue ('id', null , 'REQUEST');
  $desc  = FormUtil::getPassedValue ('desc', null, 'REQUEST');
  $asc  = FormUtil::getPassedValue ('asc', null, 'REQUEST');
  $page = FormUtil::getPassedValue ('page', 1, 'REQUEST');
  $pageSize = 2000;
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }else if(!is_numeric($id)){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","Invalid ardument: [id].");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','HouseNoArray', false)))
  return LogUtil::registerError ("Unable to load class [HouseNoArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  

  if (method_exists($objectArray,'genFilterHouseListByDistrictId')){
    $where = $objectArray->genFilterHouseListByDistrictId();
  }

  
  if((isset($desc)||isset($asc))&&($desc!='no'&&$asc!='no')){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  //Spilt page
  $totalItems  = $objectArray->getCount($where , true);
  $totalPages  = ceil($totalItems/$pageSize);
  $nowPage = $page;
  if($page>0){
    $page--;
  }
  $page = $page*$pageSize;
  
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  $i=0;
  foreach($objDataArray AS $object){
  
    $object2[$i] = $object;
    preg_match('/(?P<house_no>\w+)\/(?P<sub_house_no>\d+)/',$object['no'],$matches);
    
    $object2[$i]['sub_house_no'] = $matches['sub_house_no'] ? $matches['sub_house_no'] : "0";
    $object2[$i]['house_no'] = $matches['house_no'] ? $matches['house_no'] : $object['no'];

  $i++;
  }
  
  if($asc=='no'||(!isset($asc)&&!isset($desc))){
    $object2 = array_msort($object2,array('vil_id'=>SORT_ASC, 'vil_moo'=>SORT_ASC, 'house_no'=>SORT_ASC, 'sub_house_no'=>SORT_ASC));
  }else if ($desc=='no'){
    $object2 = array_msort($object2,array('vil_id'=>SORT_DESC, 'vil_moo'=>SORT_DESC, 'house_no'=>SORT_DESC, 'sub_house_no'=>SORT_DESC));
  }
  
  $previous = $nowPage<=1 ? "" : $nowPage-1;
  $next = $nowPage>=$totalPages ? "" : $nowPage+1;
  //Assign some attribute for xml
  $object2['totalItems'] = $totalItems;
  $object2['totalPages'] = $totalPages;
  $object2['nowPage'] = $nowPage;
  $object2['next'] = $next;
  $object2['previous'] = $previous;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($object2 as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getHouseListByTambonId()
{
  $id  = FormUtil::getPassedValue ('id', null , 'REQUEST');
  $desc  = FormUtil::getPassedValue ('desc', null, 'REQUEST');
  $asc  = FormUtil::getPassedValue ('asc', null, 'REQUEST');
  $page = FormUtil::getPassedValue ('page', 1, 'REQUEST');
  $pageSize = 2000;
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }else if(!is_numeric($id)){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","Invalid ardument: [id].");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','HouseNoArray', false)))
  return LogUtil::registerError ("Unable to load class [HouseNoArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  

  if (method_exists($objectArray,'genFilterHouseListByTambonId')){
    $where = $objectArray->genFilterHouseListByTambonId();
  }

  
  if((isset($desc)||isset($asc))&&($desc!='no'&&$asc!='no')){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  //Spilt page
  $totalItems  = $objectArray->getCount($where , true);
  $totalPages  = ceil($totalItems/$pageSize);
  $nowPage = $page;
  if($page>0){
    $page--;
  }
  $page = $page*$pageSize;
  
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  $i=0;
  foreach($objDataArray AS $object){
  
    $object2[$i] = $object;
    preg_match('/(?P<house_no>\w+)\/(?P<sub_house_no>\d+)/',$object['no'],$matches);
    
    $object2[$i]['sub_house_no'] = $matches['sub_house_no'] ? $matches['sub_house_no'] : "0";
    $object2[$i]['house_no'] = $matches['house_no'] ? $matches['house_no'] : $object['no'];

  $i++;
  }
  
  if($asc=='no'||(!isset($asc)&&!isset($desc))){
    $object2 = array_msort($object2,array('vil_id'=>SORT_ASC, 'vil_moo'=>SORT_ASC, 'house_no'=>SORT_ASC, 'sub_house_no'=>SORT_ASC));
  }else if ($desc=='no'){
    $object2 = array_msort($object2,array('vil_id'=>SORT_DESC, 'vil_moo'=>SORT_DESC, 'house_no'=>SORT_DESC, 'sub_house_no'=>SORT_DESC));
  }
  
  $previous = $nowPage<=1 ? "" : $nowPage-1;
  $next = $nowPage>=$totalPages ? "" : $nowPage+1;
  //Assign some attribute for xml
  $object2['totalItems'] = $totalItems;
  $object2['totalPages'] = $totalPages;
  $object2['nowPage'] = $nowPage;
  $object2['next'] = $next;
  $object2['previous'] = $previous;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($object2 as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getHouseListByVillageId()
{
  $id  = FormUtil::getPassedValue ('id', null , 'REQUEST');
  $desc  = FormUtil::getPassedValue ('desc', null, 'REQUEST');
  $asc  = FormUtil::getPassedValue ('asc', null, 'REQUEST');
  $page = FormUtil::getPassedValue ('page', 1, 'REQUEST');
  $pageSize = 2000;
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }else if(!is_numeric($id)){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","Invalid ardument: [id].");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','HouseNoArray', false)))
  return LogUtil::registerError ("Unable to load class [HouseNoArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  

  if (method_exists($objectArray,'genFilterHouseListByVillageId')){
    $where = $objectArray->genFilterHouseListByVillageId();
  }

  
  if((isset($desc)||isset($asc))&&($desc!='no'&&$asc!='no')){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  //Spilt page
  $totalItems  = $objectArray->getCount($where , true);
  $totalPages  = ceil($totalItems/$pageSize);
  $nowPage = $page;
  if($page>0){
    $page--;
  }
  $page = $page*$pageSize;
  
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  $i=0;
  foreach($objDataArray AS $object){
  
    $object2[$i] = $object;
    preg_match('/(?P<house_no>\w+)\/(?P<sub_house_no>\d+)/',$object['no'],$matches);
    
    $object2[$i]['sub_house_no'] = $matches['sub_house_no'] ? $matches['sub_house_no'] : "0";
    $object2[$i]['house_no'] = $matches['house_no'] ? $matches['house_no'] : $object['no'];

  $i++;
  }
  
  if($asc=='no'||(!isset($asc)&&!isset($desc))){
    $object2 = array_msort($object2,array('vil_id'=>SORT_ASC, 'vil_moo'=>SORT_ASC, 'house_no'=>SORT_ASC, 'sub_house_no'=>SORT_ASC));
  }else if ($desc=='no'){
    $object2 = array_msort($object2,array('vil_id'=>SORT_DESC, 'vil_moo'=>SORT_DESC, 'house_no'=>SORT_DESC, 'sub_house_no'=>SORT_DESC));
  }
  
  $previous = $nowPage<=1 ? "" : $nowPage-1;
  $next = $nowPage>=$totalPages ? "" : $nowPage+1;
  //Assign some attribute for xml
  $object2['totalItems'] = $totalItems;
  $object2['totalPages'] = $totalPages;
  $object2['nowPage'] = $nowPage;
  $object2['next'] = $next;
  $object2['previous'] = $previous;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($object2 as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getPersonListByVillageId()
{
  $id  = FormUtil::getPassedValue ('id', null , 'REQUEST');
  $desc  = FormUtil::getPassedValue ('desc', null, 'REQUEST');
  $asc  = FormUtil::getPassedValue ('asc', null, 'REQUEST');
  $page = FormUtil::getPassedValue ('page', 1, 'REQUEST');
  $pageSize = 2000;
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }else if(!is_numeric($id)){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","Invalid ardument: [id].");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','PersonArray', false)))
  return LogUtil::registerError ("Unable to load class [PersonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  

  if (method_exists($objectArray,'genFilterPersonByVillageId')){
    $where = $objectArray->genFilterPersonByVillageId();
  }
  
  if((isset($desc)||isset($asc))&&($desc!='no'&&$asc!='no')){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  //Spilt page
  $totalItems  = $objectArray->getCount($where , true);
  $totalPages  = ceil($totalItems/$pageSize);
  $nowPage = $page;
  if($page>0){
    $page--;
  }
  $page = $page*$pageSize;
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  $i=0;
  foreach($objDataArray AS $object){
  
    $object2[$i] = $object;
    preg_match('/(?P<house_no>\w+)\/(?P<sub_house_no>\d+)/',$object['no'],$matches);
    
    $object2[$i]['sub_house_no'] = $matches['sub_house_no'] ? $matches['sub_house_no'] : "0";
    $object2[$i]['house_no'] = $matches['house_no'] ? $matches['house_no'] : $object['no'];

  $i++;
  }
  
  if($asc=='no'||(!isset($asc)&&!isset($desc))){
    $object2 = array_msort($object2,array('vil_id'=>SORT_ASC, 'vil_moo'=>SORT_ASC, 'house_no'=>SORT_ASC, 'sub_house_no'=>SORT_ASC));
  }else if ($desc=='no'){
    $object2 = array_msort($object2,array('vil_id'=>SORT_DESC, 'vil_moo'=>SORT_DESC, 'house_no'=>SORT_DESC, 'sub_house_no'=>SORT_DESC));
  }
  
  $previous = $nowPage<=1 ? "" : $nowPage-1;
  $next = $nowPage>=$totalPages ? "" : $nowPage+1;
  //Assign some attribute for xml
  $object2['totalItems'] = $totalItems;
  $object2['totalPages'] = $totalPages;
  $object2['nowPage'] = $nowPage;
  $object2['next'] = $next;
  $object2['previous'] = $previous;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($object2 as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getPersonListByHouseId()
{
  $id  = FormUtil::getPassedValue ('id', null , 'REQUEST');
  $desc  = FormUtil::getPassedValue ('desc', null, 'REQUEST');
  $asc  = FormUtil::getPassedValue ('asc', null, 'REQUEST');
  $page = FormUtil::getPassedValue ('page', 1, 'REQUEST');
  $pageSize = 2000;
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }else if(!is_numeric($id)){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","Invalid ardument: [id].");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','PersonArray', false)))
  return LogUtil::registerError ("Unable to load class [PersonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  

  if (method_exists($objectArray,'genFilterPersonByHouseId')){
    $where = $objectArray->genFilterPersonByHouseId();
  }
  
  if((isset($desc)||isset($asc))&&($desc!='no'&&$asc!='no')){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  //Spilt page
  $totalItems  = $objectArray->getCount($where , true);
  $totalPages  = ceil($totalItems/$pageSize);
  $nowPage = $page;
  if($page>0){
    $page--;
  }
  $page = $page*$pageSize;
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  $i=0;
  foreach($objDataArray AS $object){
  
    $object2[$i] = $object;
    preg_match('/(?P<house_no>\w+)\/(?P<sub_house_no>\d+)/',$object['no'],$matches);
    
    $object2[$i]['sub_house_no'] = $matches['sub_house_no'] ? $matches['sub_house_no'] : "0";
    $object2[$i]['house_no'] = $matches['house_no'] ? $matches['house_no'] : $object['no'];

  $i++;
  }
  
  if($asc=='no'||(!isset($asc)&&!isset($desc))){
    $object2 = array_msort($object2,array('vil_id'=>SORT_ASC, 'vil_moo'=>SORT_ASC, 'house_no'=>SORT_ASC, 'sub_house_no'=>SORT_ASC));
  }else if ($desc=='no'){
    $object2 = array_msort($object2,array('vil_id'=>SORT_DESC, 'vil_moo'=>SORT_DESC, 'house_no'=>SORT_DESC, 'sub_house_no'=>SORT_DESC));
  }
  
  $previous = $nowPage<=1 ? "" : $nowPage-1;
  $next = $nowPage>=$totalPages ? "" : $nowPage+1;
  //Assign some attribute for xml
  $object2['totalItems'] = $totalItems;
  $object2['totalPages'] = $totalPages;
  $object2['nowPage'] = $nowPage;
  $object2['next'] = $next;
  $object2['previous'] = $previous;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($object2 as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getLastPersonId()
{
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','PersonArray', false)))
  return LogUtil::registerError ("Unable to load class [PersonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  //$pntables = pnDBGetTables();
  //$column  = $pntables[$objectArray->_objType.'_column'];
    
  $object2 = DBUtil::selectFieldMax($objectArray->_objType,'id');

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  $xmlChild = $xml->createElement("lastPersonId",$object2);
  $xmlDatas->appendChild($xmlChild);


  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getPersonQuantityByProvinceId()
{
  $id = FormUtil::getPassedValue ('id', FALSE, 'REQUEST');
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','PersonArray', false)))
  return LogUtil::registerError ("Unable to load class [PersonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  //$pntables = pnDBGetTables();
  //$column  = $pntables[$objectArray->_objType.'_column'];
    
  $object2 = DBUtil::selectObjectCountByID($objectArray->_objType,$id,'pro_id');

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  $xmlChild = $xml->createElement("personQuantity",$object2);
  $xmlDatas->appendChild($xmlChild);


  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getPersonQuantityByDistrictId()
{
  $id = FormUtil::getPassedValue ('id', FALSE, 'REQUEST');
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','PersonArray', false)))
  return LogUtil::registerError ("Unable to load class [PersonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  //$pntables = pnDBGetTables();
  //$column  = $pntables[$objectArray->_objType.'_column'];
    
  $object2 = DBUtil::selectObjectCountByID($objectArray->_objType,$id,'dis_id');

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  $xmlChild = $xml->createElement("personQuantity",$object2);
  $xmlDatas->appendChild($xmlChild);


  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getPersonQuantityByTambonId()
{
  $id = FormUtil::getPassedValue ('id', FALSE, 'REQUEST');
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','PersonArray', false)))
  return LogUtil::registerError ("Unable to load class [PersonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  //$pntables = pnDBGetTables();
  //$column  = $pntables[$objectArray->_objType.'_column'];
    
  $object2 = DBUtil::selectObjectCountByID($objectArray->_objType,$id,'tam_id');

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  $xmlChild = $xml->createElement("personQuantity",$object2);
  $xmlDatas->appendChild($xmlChild);


  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getPersonQuantityByVillageId()
{
  $id = FormUtil::getPassedValue ('id', FALSE, 'REQUEST');
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','PersonArray', false)))
  return LogUtil::registerError ("Unable to load class [PersonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  //$pntables = pnDBGetTables();
  //$column  = $pntables[$objectArray->_objType.'_column'];
    
  $object2 = DBUtil::selectObjectCountByID($objectArray->_objType,$id,'vil_id');

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  $xmlChild = $xml->createElement("personQuantity",$object2);
  $xmlDatas->appendChild($xmlChild);


  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getPersonQuantityByHouseId()
{
  $id = FormUtil::getPassedValue ('id', FALSE, 'REQUEST');
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','PersonArray', false)))
  return LogUtil::registerError ("Unable to load class [PersonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  //$pntables = pnDBGetTables();
  //$column  = $pntables[$objectArray->_objType.'_column'];
    
  $object2 = DBUtil::selectObjectCountByID($objectArray->_objType,$id,'hou_id');

  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($object2))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  $xmlChild = $xml->createElement("personQuantity",$object2);
  $xmlDatas->appendChild($xmlChild);


  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}


function VoteDataCenter_ws_getProvinceDetailById()
{
  $id = FormUtil::getPassedValue ('id', FALSE, 'REQUEST');
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','ProvinceArray', false)))
  return LogUtil::registerError ("Unable to load class [ProvinceArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  
  if(intval($id)){
    if (method_exists($objectArray,'genFilter')){
      $where = $objectArray->genFilter();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}



function VoteDataCenter_ws_getDistrictDetailById()
{
  $id = FormUtil::getPassedValue ('id', FALSE, 'REQUEST');
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','DistrictArray', false)))
  return LogUtil::registerError ("Unable to load class [DistrictArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  
  if(intval($id)){
    if (method_exists($objectArray,'genFilter')){
      $where = $objectArray->genFilter();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VoteDataCenter_ws_getTambonDetailById()
{
  $id = FormUtil::getPassedValue ('id', FALSE, 'REQUEST');
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','TambonArray', false)))
  return LogUtil::registerError ("Unable to load class [TambonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  
  if(intval($id)){
    if (method_exists($objectArray,'genFilter')){
      $where = $objectArray->genFilter();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}


function VoteDataCenter_ws_getVillageDetailById()
{
  $id = FormUtil::getPassedValue ('id', FALSE, 'REQUEST');
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','VillageArray', false)))
  return LogUtil::registerError ("Unable to load class [VillageArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  
  if(intval($id)){
    if (method_exists($objectArray,'genFilter')){
      $where = $objectArray->genFilter();
    }
  }
  
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }
  
  $objectArray->get($where, $sort, $page, $pageSize);
  $objDataArray = $objectArray->_objData;
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  if(empty($objDataArray))
  {
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  foreach($objDataArray as $key1=>$val1)
  {
    
    $xmlChild = $xml->createElement("data");
    if(is_array($val1)){
      foreach($val1 as $key2=>$val2){
        $xmlNode = $xml->createElement($key2,$val2);
        $xmlChild->appendChild($xmlNode);
      }
    }else{
      $xmlChild = $xml->createElement($key1,$val1);
    }

    $xmlDatas->appendChild($xmlChild);
  }

  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}



//use for generate selector
function VoteDataCenter_ws_genOption(){
  $id  = FormUtil::getPassedValue ('id', FALSE , 'GET');
  $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
  $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
  
  
  if(!$id){
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    $xmlTitle = $xml->createElement("title","argument [id] is required.");
    $xmlChild->appendChild($xmlTitle);
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
  
    print $xml->saveXML();
    pnShutDown();
  }
  
  if (!($class = Loader::loadClassFromModule ('VoteDataCenter','TambonArray', false)))
  return LogUtil::registerError ("Unable to load class [TambonArray] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  
  if (method_exists($objectArray,'genFilterTambonByProvinceId')){
      $where = $objectArray->genFilterTambonByProvinceId();
  }
  if(isset($desc)||isset($asc)){
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
  }
  if(empty($sort)){
    $sort = $objectArray->_objSort;
  }

  $objectArray->get($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;

  header("Content-type: text/html");
  if(empty($objDataArray))
  {
    pnShutDown();
  }
  echo "<select name=\"form[tam_id]\" id=\"tam_id\">";
  echo "<option value=\"0\">-</option>";
  foreach($objDataArray as $value)
  {
    echo "<option value=\"".$value["id"]."\">".$value["name"]."</option>";
  }
  echo "</select>";

  pnShutDown();
}